

function say() {
    alert("LC:897. 递增顺序搜索树");
}
say();


/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {TreeNode}
 */
var increasingBST = function(root) {
    //10:15 - 30
    var arr = []
    dfs(root, arr)
    if(arr.length == 1){
        return arr[0]
    }
    
    for(var i = 0; i < arr.length - 1; i++){
        var itemRoot = arr[i]
        var nextRoot = arr[i + 1]
        itemRoot.left = null
        itemRoot.right = nextRoot
    }

    var last = arr.pop()
    last.left = null

    return arr[0]
};


function dfs(root, arr) {
    if(root) {
        dfs(root.left, arr)
        arr.push(root)
        dfs(root.right, arr)
    }
}
